﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace GUI.BR
{
    using DAL.Entities;
    using BLL;
    using EXT;
    using DevExpress.Utils;
    public partial class frmTrahangNCC : Form
    {
        public frmTrahangNCC()
        {
            InitializeComponent();
        }

        HanghoaBLL Hanghoa_BLL = new HanghoaBLL();

        #region F
        Guid Id_Hanghoa = Guid.Empty;
        Guid Id_Dvt = Guid.Empty;
        string TenDvt = "";
        float Soluong = 0;
        int Gianhap = 0;
        int CK = 0;
        int Thanhtien = 0;
        float Doi = 1;
        #endregion


        private void Load_Hanghoa_ForView()
        {
            HanghoaBLL Hanghoa_BLL = new HanghoaBLL();
            var dt = Hanghoa_BLL.Hanghoa_Select_All_Vali_ForView0();
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    gleHanghoa.DataSource = dt;
                }
            }
        }

        DataTable dtTrahangNCC = new DataTable();

        private void TaoTable()
        {
            DataColumn Id_Hanghoa = new DataColumn("Id_Hanghoa", typeof(Guid));
            DataColumn Id_Dvt = new DataColumn("Id_Dvt", typeof(Guid));
            DataColumn TenDvt = new DataColumn("TenDvt", typeof(string));
            DataColumn Soluong = new DataColumn("Soluong", typeof(float));
            DataColumn Gianhap = new DataColumn("Gianhap", typeof(int));
            DataColumn CK = new DataColumn("CK", typeof(int));
            DataColumn Thanhtien = new DataColumn("Thanhtien", typeof(int));
            DataColumn Doi = new DataColumn("Doi", typeof(float));
            dtTrahangNCC.Columns.Add(Id_Hanghoa);
            dtTrahangNCC.Columns.Add(Id_Dvt);
            dtTrahangNCC.Columns.Add(TenDvt);
            dtTrahangNCC.Columns.Add(Soluong);
            dtTrahangNCC.Columns.Add(Gianhap);
            dtTrahangNCC.Columns.Add(CK);
            dtTrahangNCC.Columns.Add(Thanhtien);
            dtTrahangNCC.Columns.Add(Doi);
            gcTrahangNCC.DataSource = dtTrahangNCC;
        }

        private void frmTrahangNCC_Load(object sender, EventArgs e)
        {
            WaitDialogForm wdf = new WaitDialogForm("Đang load dữ liệu...", "Xin chờ trong giây lát.");
            Load_Hanghoa_ForView();
            TaoTable();
            wdf.Close();
        }

        private void Load_Dvt(Guid Id_Hanghoa)
        {
            cbxDvt.Items.Clear();
            var dt = Hanghoa_BLL.Hanghoa_Select_By_Id_Hanghoa(Id_Hanghoa);
            cbxDvt.Items.Add(dt.Rows[0]["TenDvt0"].ToString());
            if (dt.Rows[0]["Id_Dvt1"].GetGuid() != Guid.Empty)
            {
                cbxDvt.Items.Add(dt.Rows[0]["TenDvt1"].ToString());
            }
            if (dt.Rows[0]["Id_Dvt2"].GetGuid() != Guid.Empty)
            {
                cbxDvt.Items.Add(dt.Rows[0]["TenDvt2"].ToString());
            }
        }

        private void gleHanghoa_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            try
            {
                DataRow[] dr = ((DataTable)gleHanghoa.DataSource).Select(String.Format("Id_Hanghoa ='{0}'", e.NewValue));
                if (dr.Length > 0)
                {
                    Id_Hanghoa = e.NewValue.GetGuid();
                    Id_Dvt = dr[0]["Id_Dvt"].GetGuid();
                    TenDvt = dr[0]["TenDvt"].ToString();
                    Gianhap = dr[0]["Gianhap"].ToInt32();
                    CK = dr[0]["CK"].ToInt32();

                    gvTrahangNCC.SetFocusedRowCellValue(colTenDvt, Id_Dvt);
                    gvTrahangNCC.SetFocusedRowCellValue(colTenDvt, TenDvt);
                    gvTrahangNCC.SetFocusedRowCellValue(colSoluong, 0);
                    gvTrahangNCC.SetFocusedRowCellValue(colGianhap, Gianhap);
                    gvTrahangNCC.SetFocusedRowCellValue(colThanhtien, 0);
                    gvTrahangNCC.SetFocusedRowCellValue(colCK, CK);
                    gvTrahangNCC.SetFocusedRowCellValue(colDoi, Doi);
                    gvTrahangNCC.UpdateCurrentRow();
                }
            }
            catch (Exception a)
            {
            }
        }

        private void gvTrahangNCC_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            gvTrahangNCC.SetRowCellValue(e.RowHandle,colId_Dvt, Id_Dvt);
            gvTrahangNCC.SetRowCellValue(e.RowHandle, colTenDvt, TenDvt);
            gvTrahangNCC.SetRowCellValue(e.RowHandle,colSoluong, 0);
            gvTrahangNCC.SetRowCellValue(e.RowHandle,colGianhap, Gianhap);
            gvTrahangNCC.SetRowCellValue(e.RowHandle,colThanhtien, 0);
            gvTrahangNCC.SetRowCellValue(e.RowHandle,colCK, CK);
            gvTrahangNCC.SetRowCellValue(e.RowHandle,colDoi, Doi);
            Load_Dvt(Id_Hanghoa);
        }

        private void gvTrahangNCC_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                DataRow dr = gvTrahangNCC.GetFocusedDataRow();
                Load_Dvt(dr["Id_Hanghoa"].GetGuid());
            }
            catch (Exception)
            {
            }
        }

        private void gvTrahangNCC_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            DataRow dr = gvTrahangNCC.GetFocusedDataRow();
            //Chỉ thay đổi số lượng
            if (e.Column.ToString()=="Số lượng")
            {
                int tt = (int)(e.Value.ToFloat()/100 * dr["Gianhap"].ToInt32());
                tt -= tt / 100 * dr["CK"].ToInt32();
                gvTrahangNCC.SetFocusedRowCellValue(colThanhtien, tt);
            }
            //Chỉ thay đổi giá
            if (e.Column.ToString() == "Giá nhập")
            {
                int tt = (int)(dr["Soluong"].ToFloat() * e.Value.ToInt32());
                tt -= tt / 100 * dr["CK"].ToInt32();
                gvTrahangNCC.SetFocusedRowCellValue(colThanhtien, tt);
            }
            //Chỉ thay đổi CK
            if (e.Column.ToString() == "%CK")
            {
                int tt = (int)(dr["Soluong"].ToFloat() * dr["Gianhap"].ToInt32());
                tt -= tt / 100 * e.Value.ToInt32();
                gvTrahangNCC.SetFocusedRowCellValue(colThanhtien, tt);
            }
            //Chỉ thay đổi đơn vị tính
            if (e.Column.ToString() == "Đvt")
            {
                float DoiX = dr["Doi"].ToFloat();
                float GianhapX = dr["Gianhap"].ToInt32()/DoiX;
                DataTable dt = Hanghoa_BLL.Hanghoa_Select_By_Id_Hanghoa(Id_Hanghoa);
                if (e.Value.ToString()==dt.Rows[0]["TenDvt0"].ToString())
                {
                    gvTrahangNCC.SetRowCellValue(e.RowHandle, colId_Dvt, dt.Rows[0]["Id_Dvt0"].GetGuid());
                    gvTrahangNCC.SetRowCellValue(e.RowHandle, colDoi, 1);
                    Gianhap = (int)GianhapX;
                    gvTrahangNCC.SetRowCellValue(e.RowHandle, colGianhap, Gianhap);
                    int tt = (int)(dr["Soluong"].ToFloat() * Gianhap);
                    tt -= tt / 100 * dr["CK"].ToInt32();
                    gvTrahangNCC.SetFocusedRowCellValue(colThanhtien, tt);
                    return;
                }
                if (e.Value.ToString() == dt.Rows[0]["TenDvt1"].ToString())
                {
                    gvTrahangNCC.SetRowCellValue(e.RowHandle, colId_Dvt, dt.Rows[0]["Id_Dvt1"].GetGuid());
                    gvTrahangNCC.SetRowCellValue(e.RowHandle, colDoi, dt.Rows[0]["Doi1"].ToFloat());
                    Gianhap = (int)(GianhapX * dt.Rows[0]["Doi1"].ToFloat());
                    gvTrahangNCC.SetRowCellValue(e.RowHandle, colGianhap, Gianhap);
                    int tt = (int)(dr["Soluong"].ToFloat() * Gianhap);
                    tt -= tt / 100 * dr["CK"].ToInt32();
                    gvTrahangNCC.SetFocusedRowCellValue(colThanhtien, tt);
                    return;
                }
                if (e.Value.ToString() == dt.Rows[0]["TenDvt2"].ToString())
                {
                    gvTrahangNCC.SetRowCellValue(e.RowHandle, colId_Dvt, dt.Rows[0]["Id_Dvt2"].GetGuid());
                    gvTrahangNCC.SetRowCellValue(e.RowHandle, colDoi, dt.Rows[0]["Doi2"].ToFloat());
                    Gianhap = (int)(GianhapX * dt.Rows[0]["Doi2"].ToFloat());
                    gvTrahangNCC.SetRowCellValue(e.RowHandle, colGianhap, Gianhap);
                    int tt = (int)(dr["Soluong"].ToFloat() * Gianhap);
                    tt -= tt / 100 * dr["CK"].ToInt32();
                    gvTrahangNCC.SetFocusedRowCellValue(colThanhtien, tt);
                }
            }
        }
    }
}
